-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_Insert]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_Update]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_Delete]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_Load]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_KhoHang_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_KhoHang_SelectAll]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_Insert]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_Insert]
	@MaKho varchar(10),
	@TenKho nvarchar(1000),
	@DiaChi nvarchar(1000),
	@GhiChu nvarchar(max)
AS
INSERT INTO [dbo].[t_WM_KhoHang]
(
	[MaKho],
	[TenKho],
	[DiaChi],
	[GhiChu]
)
VALUES
(
	@MaKho,
	@TenKho,
	@DiaChi,
	@GhiChu
)

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_Update]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_Update]
	@MaKho varchar(10),
	@TenKho nvarchar(1000),
	@DiaChi nvarchar(1000),
	@GhiChu nvarchar(max)
AS

UPDATE
	[dbo].[t_WM_KhoHang]
SET
	[TenKho] = @TenKho,
	[DiaChi] = @DiaChi,
	[GhiChu] = @GhiChu
WHERE
	[MaKho] = @MaKho

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_InsertUpdate]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_InsertUpdate]
	@MaKho varchar(10),
	@TenKho nvarchar(1000),
	@DiaChi nvarchar(1000),
	@GhiChu nvarchar(max)
AS
IF EXISTS(SELECT [MaKho] FROM [dbo].[t_WM_KhoHang] WHERE [MaKho] = @MaKho)
	BEGIN
		UPDATE
			[dbo].[t_WM_KhoHang] 
		SET
			[TenKho] = @TenKho,
			[DiaChi] = @DiaChi,
			[GhiChu] = @GhiChu
		WHERE
			[MaKho] = @MaKho
	END
ELSE
	BEGIN
	INSERT INTO [dbo].[t_WM_KhoHang]
	(
			[MaKho],
			[TenKho],
			[DiaChi],
			[GhiChu]
	)
	VALUES
	(
			@MaKho,
			@TenKho,
			@DiaChi,
			@GhiChu
	)	
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_Delete]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_Delete]
	@MaKho varchar(10)
AS

DELETE FROM 
	[dbo].[t_WM_KhoHang]
WHERE
	[MaKho] = @MaKho

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_DeleteDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_WM_KhoHang] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_Load]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_Load]
	@MaKho varchar(10)
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[MaKho],
	[TenKho],
	[DiaChi],
	[GhiChu]
FROM
	[dbo].[t_WM_KhoHang]
WHERE
	[MaKho] = @MaKho
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_SelectDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[MaKho],
	[TenKho],
	[DiaChi],
	[GhiChu]
FROM [dbo].[t_WM_KhoHang] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_KhoHang_SelectAll]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_KhoHang_SelectAll]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[MaKho],
	[TenKho],
	[DiaChi],
	[GhiChu]
FROM
	[dbo].[t_WM_KhoHang]	

GO

